(function($,window){
    window.MenuBar = {};
    MenuBar.curPage = {};
    // 初始路径参数
    MenuBar.centerOpts = {
        // parentName:'',
        // panentURL:'main/',
        // curTitle:'',
        // curHash:'main/main_center',
        defaultURL : 'main/main',
        hashContent:''
    };

    // 新增编辑弹框初始化
    MenuBar.init = function(){
        MenuBar.hasHash = document.location.hash;
        // 默认登录首页
        if(!this.hasHash) {
            document.location.hash = MenuBar.centerOpts.defaultURL;
        }

        // 绑定a链接页面跳转地址--菜单栏、头部代办及头部面包屑首页
        MenuBar.bindHashAddress('#leftMenu,#agency,#crumbs,#operateDocBtn');

        // 动态创建菜单
        MenuBar.createMenu();

        // 监测描点变化 改变layoutCenter的值
        $(window).on('hashchange', function(e){
            // 防止浏览器地址回退时出现hash为空的情况
            if(document.location.hash == '') {
                document.location.hash = MenuBar.centerOpts.defaultURL;
            };
            MenuBar.centerOpts.hashContent = (document.location.hash).slice(1);
            MenuBar.centerOpts.curURL = MenuBar.centerOpts.hashContent +'.html';
            if(MenuBar.centerOpts) {
                // 刷新center内容
                $('#centerLayout').panel('refresh', "../" + MenuBar.centerOpts.curURL);
            
                if(MenuBar.centerOpts.curURL.indexOf("?")>-1){
                    
                    MenuBar.centerOpts.curURL=MenuBar.centerOpts.curURL.slice(0,MenuBar.centerOpts.curURL.indexOf("?"));
                };

                //面包屑
                var crumbs = {};
                crumbs.crumbsObj= $("#crumbs");
                crumbs.parentName = MenuBar.centerOpts.hashContent.split('/');
                crumbs.parentTit = $('.'+ crumbs.parentName[0] +'>a').html();
                crumbs.childTit = $('#leftMenu a[center-hash="'+ MenuBar.centerOpts.hashContent +'"]').html();
                if(crumbs.childTit && crumbs.parentTit){
                    $('#crumbsCon').html('<label style="color:#1558a6;">>&nbsp;' + crumbs.parentTit + '</label><label>>&nbsp;' + crumbs.childTit + '</label>');
                }else if(crumbs.parentTit){
                    $('#crumbsCon').html('<label style="color:#1558a6;">>&nbsp;' + crumbs.parentTit + '</label>');
                }else if(crumbs.childTit && crumbs.childTit !== '首页'){
                    $('#crumbsCon').html('<label>>&nbsp;' + crumbs.childTit + '</label>');
                }else{
                    $('#crumbsCon').html('');
                };

                editIndex = undefined;

                // 获取当前center页面权限
                MenuBar.centerOpts.menuAttr = $('#leftMenu a[center-hash="'+ MenuBar.centerOpts.hashContent +'"]').attr('center-menu');
            }
         }).trigger('hashchange');


        // 多级菜单
        // $('#leftMenu li').hover(
        //     function(){
        //         if(this.timeout){clearTimeout(this.timeout)}
        //         // 子菜单获取;
        //         var childMenu = $(this).find('>ul');

        //         // 获取子菜单position:left和top;
        //         var positionTop = childMenu.attr('options-top') || 0;
        //         var offset = $(this).offset();

        //         childMenu.css({
        //             top: offset.top + parseInt(positionTop), 
        //             left: offset.left + $(this).outerWidth() 
        //         }).show();
        //     },
        //     function(){
        //         var _self = this;
        //         this.timeout = setTimeout(function(){
        //             $(_self).find('>ul').hide();
        //         }, 200)
        //     }
        // );
        // 
            var eles =$("#navLeft>ul>li");
            eles.each(function(index, element) {
                eles.eq(index).click(function(){
                    $(this).find(">a").addClass("on").end().siblings().find(">a").removeClass("on"); 
                    $(this).find("ul").show(100).end().siblings().find("ul").hide(100);

                });
            });

            //二级菜单
            var eles_sec =$("#navLeft>ul>li>ul>li");
            eles.each(function(index, element) {
                eles.eq(index).click(function(){
                    $(this).find(">a").addClass("on").end().siblings().find(">a").removeClass("on");
                    $(this).find("ul").show(100).end().siblings().find("ul").hide(100);
                });
            });
    };

    // 绑定a链接页面跳转地址
    MenuBar.bindHashAddress = function(parentEle) {
        $(parentEle).on('click', 'a', function(){
            // 取得属性值转换为json对象
            var centerStr = $(this).attr('center-hash');
            if(centerStr){
                // var centerOptsArr=centerStr.split(',');
                // for(var i = 0;i < centerOptsArr.length;i++){
                //     var centerOptArr = centerOptsArr[i].split(':');
                //     MenuBar.centerOpts[centerOptArr[0]] = centerOptArr[1].slice(1,-1);
                // }
                // document.location.hash = MenuBar.centerOpts.curHash;
                document.location.hash = centerStr;
            }
        });
    };

    // 动态创建菜单
    MenuBar.createMenu = function(){
        var menuOpts = [
            {
                name:"gameUser",
                title:"用户管理",
                classStyle:"navico_system",
                secondMenu:[
                    {title:'用户管理',centerHash:"gameUser/userManage"},
                    {title:'实名认证管理',centerHash:"gameUser/gameUserForAnth"},
                    {title:'后台账号配置',centerHash:"gameUser/account"},
                    {title:'角色配置',centerHash:"gameUser/role"},
                    {title:'权限配置',centerHash:"gameUser/authority"},
                    {title:'资源配置',centerHash:"gameUser/resource"}
                ]
            },
            {
                name:"gameManage",
                title:"游戏管理",
                classStyle:"navico_meau",
                secondMenu:[
                    {title:'游戏分类',centerHash:"gameManage/gameType"},
                    {title:'礼包管理',centerHash:"gameManage/giftManage"},
                    {title:'攻略管理',centerHash:"gameManage/strategyManage"},
                    {title:'专题管理',centerHash:"gameManage/subjectManage"}
                ]
            },
            {
                name:"communityManage",
                title:"社区管理",
                classStyle:"navico_info",
                secondMenu:[
                    {title:'游戏圈管理',centerHash:"communityManage/gameHubManage"},
                    {title:'公会管理',centerHash:"communityManage/unionsManage"}
                ]
            },
            {
                name:"system",
                title:"系统基础设置",
                classStyle:"navico_article",
                secondMenu:[
                    {title:'经验值规则',centerHash:"system/experienceRules"},
                    {title:'轮播广告',centerHash:"system/carouselAd"},
                    {title:'系统消息',centerHash:"system/systemInfo"},
                    {title:'文章管理',centerHash:"system/aticleManage"},
                    {title:'客服管理',centerHash:"system/serviceManage"}
                ]
            }
        ];

        // 循环遍历后台返回值  根据返回值判断显示菜单栏
        for(var i = 0; i < menuOpts.length; i++){
            if(menuOpts[i]['secondMenu']){
                var secondMenuLen = menuOpts[i]['secondMenu'].length;
                $('#leftMenu').append('<li class="'+ menuOpts[i].name +'"><a href="javascript:void(0);"><i class="'+ menuOpts[i].classStyle +'"></i>'+ menuOpts[i].title +'</a><ul></ul></li>');
                for(var j = 0; j < secondMenuLen;j++){
                    $('#leftMenu .'+menuOpts[i].name + '>ul').append('<li><a href="javascript:void(0);" center-hash="'+ menuOpts[i]['secondMenu'][j].centerHash +'" center-menu="' + menuOpts[i]['secondMenu'][j].centerMenu + '">'+ menuOpts[i]['secondMenu'][j].title +'</a></li>');
                };
            }else{
                $('#leftMenu').append('<li><a href="javascript:void(0);" center-hash="'+ menuOpts[i].centerHash +'" center-menu="' + menuOpts[i].centerMenu + '">'+ menuOpts[i].title +'</a><ul></ul></li>');
            };
        };
    };

})(jQuery,window);